$(function () {
    var searchPage = function () {
        this.init();
    };
    searchPage.prototype = {
        // 初始化
        init: function () {
            this.loadhotgoods();
            this.bindEvent();
        },

        // 热门商品
        loadhotgoods: function () {
            $.ajax({
                type: "GET",
                url: CONFIG_MANAGER.RequstUrl + "v1/goods/search/hottag/18"
            }).done(function (res) {
                if (res && res.length > 0) {
                    var html = [];
                    $.each(res, function (i, tagitem) {
                        html.push("<li tip='" + tagitem.catagory_no + "'>" + tagitem.name + "</li>")
                    });
                    $("#hotwordlistsearch").html(html.join(""));

                }
            });

            var seachtag = getLocalStorage(STORAGE_MANAGER.searchtag);
            if (seachtag) {
                $("#searchtext").attr("placeholder", seachtag.name);
            }
        },

        // 绑定事件
        bindEvent: function () {
            var model = this;
            // 回退
            $("#btncloseseahpage").on("tap", function () {
                parent.closeSearchPage();
            });

            // 搜索
            $("#btnsearch").on("tap", function () {
                var searchword = $("#searchtext").val();
                setSeachHistoryStorage(searchword);

                model.search(searchword);
            });

            // 热门搜索
            $("#hotwordlistsearch").on("tap", "li", function () {
                var searchword = $(this).attr("tip") + "|" + $(this).text();
                setSeachHistoryStorage(searchword);

                model.search(searchword);
            });

            // 历史纪录
            $("#searchhistorylist").on("tap", "p[name=historysearch]", function () {
                var searchword = $(this).text();

                if ($(this).attr("tip")) {
                    searchword = $(this).attr("tip") + "|" + $(this).text();
                }

                setSeachHistoryStorage(searchword);
                model.search(searchword);
            });

            // 删除历史纪录
            $("#searchhistorylist").on("tap", "p[name=delhistory]", function () {
                var searchtext = $(this).parent().find('p[name=historysearch]').text();
                var list = JSON.parse(localStorage.getItem(STORAGE_MANAGER.searchhistory));
                var itemindex = list.indexOf(searchtext);
                if (itemindex > -1) {
                    list.splice(itemindex, 1);
                }
                localStorage.setItem(STORAGE_MANAGER.searchhistory, JSON.stringify(list));

                $(this).parent().remove();
            });

            $("#searchtext").on("keydown", function (e) {
                e = e || event;
                if (e.keyCode == 13) {  //判断是否单击的enter按键(回车键)
                    var searchword = $("#searchtext").val();
                    setSeachHistoryStorage(searchword);

                    model.search(searchword);
                }
            });


            function setSeachHistoryStorage(searchtext) {
                searchtext = searchtext.trim();
                $("#searchtext").val(searchtext);
                $("#searchtext").blur();
                if (searchtext == "") {
                    return;
                }
                var searchhistory = getLocalStorage(STORAGE_MANAGER.searchhistory);
                var list = [];
                if (searchhistory) {
                    list = searchhistory;
                }
                var itemindex = list.indexOf(searchtext);
                if (itemindex > -1) {
                    list.splice(itemindex, 1);
                }
                list.unshift(searchtext);
                setLocalStorage(STORAGE_MANAGER.searchhistory, list);
            }
        },

        // 选择条件后回掉函数
        search: function (searchword) {
            var searchtag = $("#searchtext").attr("placeholder");
            if (searchtag.indexOf("搜索您喜欢的店铺") == -1 && !searchword) {
                searchword = searchtag;
            }
            $("#searchtext").val("");
            var currentpage = window.parent.window.location.href;
            // 如果是商品列表页面
            if (currentpage.indexOf("goodslist.html") > -1 || currentpage.indexOf("shop.html") > -1) {
                parent.search(searchword);
                return;
            }

            if (searchword.indexOf('|') > -1) {
                var catory = searchword.split('|');
                var redirecturl = "../goods/goodslist.html?catalog={0}&searchword={1}".format(catory[0], catory[1]);
                redirect(redirecturl);
            } else {
                redirect("../goods/goodslist.html?searchword=" + searchword);
            }
        }
    };

    var _searchPage = new searchPage();
});
function show(searchword) {
    setTimeout(function () {
        $("#searchtext").focus();
    }, 300);

    $("#searchtext").val(searchword);
}

// 历史纪录
function loadSearchHistory() {
    var list = getLocalStorage(STORAGE_MANAGER.searchhistory);
    if (!list) {
        return;
    }
    if (list.length > 10) {
        list.splice(10);
    }
    var html = [];
    for (var i = 0; i < list.length; i++) {
        html.push("<div class='mbbottom lsjl'>");
        if (list[i].indexOf("|") > -1) {
            var catory = list[i].split('|');
            html.push("<p class='lsjlny floatl heizi' tip='" + catory[0] + "' name='historysearch'>" + catory[1] + "</p>");
        } else {
            html.push("<p class='lsjlny floatl heizi' name='historysearch'>" + list[i] + "</p>");
        }
        html.push("<p class='chahao' name='delhistory'><img src='../../images/chahao.png' /></p>");
        html.push("</div>");
    }
    $("#searchhistorylist").html(html.join(""));
}
